{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9936704199323058\n",
      "7\n",
      "400\n",
      "12.498793377755582\n",
      "10\n",
      "[40, 30, 20, 10, 50]\n",
      "[40, 50]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import random\n",
    "\n",
    "# 1. 产生随机的数字\n",
    "\n",
    "# 0~1 随机小数包含0 不包含1\n",
    "print(random.random())\n",
    "# 1-10 包含1 包含10\n",
    "print(random.randint(1,10))\n",
    "# 起始值 终止值 步长\n",
    "print(random.randrange(100,1000,100))\n",
    "# \n",
    "print(random.uniform(10.0,20.0))\n",
    "\n",
    "\n",
    "# 2. 对列表操作\n",
    "ls = [10,20,30,40,50]\n",
    "# 随机选一个\n",
    "print(random.choice(ls))\n",
    "# 打乱\n",
    "random.shuffle(ls)\n",
    "print(ls)\n",
    "# 随机取出几个\n",
    "print(random.sample(ls,2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1456692551041303\n",
      "0.45492700451402135\n",
      "0.7707838056590222\n",
      "0.705513226934028\n"
     ]
    }
   ],
   "source": [
    "random.seed(100)\n",
    "# 如果种子相同 随机序列是固定的\n",
    "print(random.random()) \n",
    "print(random.random())\n",
    "print(random.random())\n",
    "print(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lTG3\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "# 产生4位随机验证码\n",
    "\n",
    "# 1. 定义一个序列 数字+英文字母\n",
    "s = \"0123456789\"\n",
    "# 英文字母的ASCII65~122之间 \n",
    "# print(chr(122))\n",
    "# 大写字母\n",
    "for i in range(65,65+26):\n",
    "    s=s+chr(i)\n",
    "# 小写字母    \n",
    "for i in range(97,97+26):\n",
    "    s=s+chr(i)\n",
    "# 随机从序列中取出4个,连接成字符串\n",
    "print(\"\".join(random.sample(s,4)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1红包6.38元,剩余:93.62\n",
      "第2红包1.81元,剩余:91.81\n",
      "第3红包6.03元,剩余:85.78\n",
      "第4红包1.48元,剩余:84.3\n",
      "第5红包8.51元,剩余:75.79\n",
      "第6红包1.63元,剩余:74.16\n",
      "第7红包1.67元,剩余:72.49\n",
      "第8红包0.26元,剩余:72.23\n",
      "第9红包6.66元,剩余:65.57\n",
      "第10红包9.7元,剩余:55.87\n",
      "第11红包7.99元,剩余:47.88\n",
      "第12红包9.22元,剩余:38.66\n",
      "第13红包3.88元,剩余:34.78\n",
      "第14红包6.24元,剩余:28.54\n",
      "第15红包7.57元,剩余:20.97\n",
      "第16红包0.52元,剩余:20.45\n",
      "第17红包2.47元,剩余:17.98\n",
      "第18红包6.19元,剩余:11.79\n",
      "第19红包9.28元,剩余:2.51\n",
      "第20红包7.14元,剩余:0\n"
     ]
    }
   ],
   "source": [
    "# 实例\n",
    "import random\n",
    "\n",
    "total = 100\n",
    "count = 0\n",
    "\n",
    "while True:\n",
    "    t = random.uniform(0,10)    # 产生0~10\n",
    "    t = round(t,2)              # 保留小数2位\n",
    "    if total>t:\n",
    "        total -= t              # 扣掉发出的钱\n",
    "        count += 1              # 红包数+1\n",
    "        print(\"第{}红包{}元,剩余:{}\".format(count,t,round(total,2)))\n",
    "    else:\n",
    "        count += 1 \n",
    "        print(\"第{}红包{}元,剩余:{}\".format(count,round(t,2),0))\n",
    "        break\n",
    "    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
